#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from mitmproxy import http

class TestProxy(object):

    def __init__(self):
        pass
    
    def request(self, flow: http.HTTPFlow) -> None:
        if flow.request.method == "CONNECT":
            print('flow.request.method CONNECT')
            return

        # 动态修改请求的头信息
        flow.request.headers["test-header"] = "samge-20220424"
        print(flow.request.headers)

        # 这里可以动态配置二级代理的ip地址和端口
        proxy = ('192.168.2.130','8222')
        proxy = ('192.168.3.169','7890')
        proxy = ('192.168.3.169','8888')
        if flow.live:
            print(f'update flow.live proxy: {proxy}')
            flow.live.change_upstream_proxy_server(proxy)
        else:
            print('no flow.live')

    def response(self, flow: http.HTTPFlow) -> None:
        pass


addons = [
    TestProxy()
]
